#!/bin/sh
# PCP QA Test No. 517
# Test logic for config file migration, harvesting, cleanup as
# part of a packaged install.
#
# Copyright (c) 2012 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

status=1	# failure is the default!
trap "rm -rf $tmp.*; exit \$status" 0 1 2 3 15

# ... copied directly from ../debian/cleanconfigs
#

# Function to do all of the configuration file migration work
#
_clean_configs()
{
    #
    # Usage: _clean_configs [-v] new_dir old_dir ...
    #
    # Across all the files in the new_dir and old_dir args, match
    # names and pick the most recently modified version and leave
    # this (same mode and modification date) in new_dir
    #
    # -v option is verbose mode for debugging
    #

    _verbose=false
    if [ $# -gt 0 -a X"$1" = "X-v" ]
    then
	_verbose=true
	shift
    fi

    if [ $# -lt 2 ]
    then
	echo >&2 "Usage: _clean_configs [-v] new_dir old_dir ..."
	return
    fi

    _new="$1"
    if [ ! -d "$_new" ]
    then
	$verbose && echo >&2 + mkdir -p "$_new"
	mkdir -p "$_new"
    fi

    shift
    for _dir
    do
	[ "$_dir" = "$_new" ] && continue
	if [ -d "$_dir" ]
	then
	    ( cd "$_dir" ; find . -type f -print ) \
	    | sed -e 's/^\.\///' \
	    | while read _file
	    do
		_want=false
		if [ -f "$_new/$_file" ]
		then
		    # file exists in both directories, pick the more
		    # recently modified one
		    #
		    _try=`find "$_dir/$_file" -newer "$_new/$_file" -print`
		    [ -n "$_try" ] && _want=true
		else
		    _want=true
		fi
		if $_want
		then
		    _dest=`dirname $_new/$_file`
		    if [ ! -d "$_dest" ]
		    then
			$verbose && >&2 echo + mkdir "$_dest"
			mkdir "$_dest"
		    fi
		    $_verbose && echo >&2 + cp -p "$_dir/$_file" "$_new/$_file"
		    cp -p "$_dir/$_file" "$_new/$_file"
		fi
	    done
	fi
    done
}


_filter()
{
    sed \
	-e "s;$tmp;/TMP;g" \
    | LC_COLLATE=POSIX sort
}

_show()
{
    cd $1
    find * -type f \
    | while read x
    do
	$PCP_ECHO_PROG $PCP_ECHO_N "$x: ""$PCP_ECHO_C"
	cat $x
    done
    cd $here
}

# real QA test starts here
echo "Usage cases ..."
_clean_configs
_clean_configs "one_arg"
_clean_configs -v "-v+one_arg"

echo; echo "No dirs exist"
_clean_configs -v $tmp.new old1 old2 2>&1 | _filter

echo; echo "Empty new dir, harvest all, no name matches"
mkdir $tmp.old1
mkdir $tmp.old2
echo foo >$tmp.old1/foo
echo bar >$tmp.old1/bar
echo mumble >$tmp.old2/mumble
echo fumble >$tmp.old2/fumble
echo stumble >$tmp.old2/stumble
_clean_configs -v $tmp.new $tmp.old1 $tmp.old2 2>&1 | _filter
_show $tmp.new

yesterday=`pmdate -1d %Y%m%d%H%M`
echo; echo "All names match, some older files"
touch -t $yesterday $tmp.new/mumble
echo "newer mumble" >$tmp.old2/mumble
touch -t $yesterday $tmp.old1/bar
echo "newer bar" >$tmp.new/bar
_clean_configs -v $tmp.new $tmp.old1 $tmp.old2 2>&1 | _filter
_show $tmp.new

echo; echo "Hybid cases"
echo "older bar" >$tmp.old2/bar
touch -t $yesterday $tmp.old2/bar
rm $tmp.new/foo
echo "older foo" >$tmp.old1/foo
touch -t $yesterday $tmp.old1/foo
echo foo >$tmp.old2/foo
rm $tmp.new/fumble
_clean_configs -v $tmp.new $tmp.old1 $tmp.old2 2>&1 | _filter
_show $tmp.new

status=0
exit
